package com.ruoyi.doctor.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.doctor.domain.DoctorFollow;
import com.ruoyi.doctor.service.IDoctorFollowService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 医生关注管理Controller
 * 
 * @author ruoyi
 * @date 2025-08-13
 */
@RestController
@RequestMapping("/doctor/follow")
public class DoctorFollowController extends BaseController
{
    @Autowired
    private IDoctorFollowService doctorFollowService;

    /**
     * 查询医生关注管理列表
     */
    @PreAuthorize("@ss.hasPermi('doctor:follow:list')")
    @GetMapping("/list")
    public TableDataInfo list(DoctorFollow doctorFollow)
    {
        startPage();
        List<DoctorFollow> list = doctorFollowService.selectDoctorFollowList(doctorFollow);
        return getDataTable(list);
    }

    /**
     * 导出医生关注管理列表
     */
    @PreAuthorize("@ss.hasPermi('doctor:follow:export')")
    @Log(title = "医生关注管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DoctorFollow doctorFollow)
    {
        List<DoctorFollow> list = doctorFollowService.selectDoctorFollowList(doctorFollow);
        ExcelUtil<DoctorFollow> util = new ExcelUtil<DoctorFollow>(DoctorFollow.class);
        util.exportExcel(response, list, "医生关注管理数据");
    }

    /**
     * 获取医生关注管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('doctor:follow:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(doctorFollowService.selectDoctorFollowById(id));
    }

    /**
     * 查询患者是否关注医生
     */
    @GetMapping("/status")
    public AjaxResult getFollowStatus(Long doctorId, Long patientId) {
        // 调用 service 查询
        DoctorFollow query = new DoctorFollow();
        query.setDoctorId(doctorId);
        query.setPatientId(patientId);
        query.setDel(0);

        List<DoctorFollow> list = doctorFollowService.selectDoctorFollowList(query);
        if (list != null && !list.isEmpty()) {
            DoctorFollow follow = list.get(0);
            return success(new java.util.HashMap<String, Object>() {{
                put("isFollowed", true);
                put("followId", follow.getId()); // 注意：字段名可能是 doctorFollowId，改成你表里主键字段
            }});
        }

        return success(new java.util.HashMap<String, Object>() {{
            put("isFollowed", false);
            put("followId", null);
        }});
    }

    /**
     * 新增医生关注管理
     */
    @PreAuthorize("@ss.hasPermi('doctor:follow:add')")
    @Log(title = "医生关注管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DoctorFollow doctorFollow)
    {
        int rows = doctorFollowService.insertDoctorFollow(doctorFollow);
        if (rows > 0) {
            // MyBatis 插入后会自动把主键id回填到 doctorFollow 对象
            return success(doctorFollow);
        }
        return error("新增失败");
    }

    /**
     * 修改医生关注管理
     */
    @PreAuthorize("@ss.hasPermi('doctor:follow:edit')")
    @Log(title = "医生关注管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DoctorFollow doctorFollow)
    {
        return toAjax(doctorFollowService.updateDoctorFollow(doctorFollow));
    }

    /**
     * 删除医生关注管理
     */
    @PreAuthorize("@ss.hasPermi('doctor:follow:remove')")
    @Log(title = "医生关注管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(doctorFollowService.deleteDoctorFollowByIds(ids));
    }

    /*患者查询自己关注的医生列表*/
    /*@PreAuthorize("@ss.hasPermi('doctor:follow:list-my-doctors')")
    @GetMapping("/my-followed-doctors")
    public TableDataInfo getMyFollowedDoctors(Long patientId){
        DoctorFollow doctorFollow = new DoctorFollow();
        doctorFollow.setPatientId(patientId);
        doctorFollow.setDel(0);
        List<DoctorFollow> doctorFollows = doctorFollowService.selectDoctorFollowList(doctorFollow);
        return getDataTable(doctorFollows);
    }*/
    /*医生查询关注自己的患者列表*/
    /*@PreAuthorize("@ss.hasPermi('doctor:follow:list-my-patients')")
    @GetMapping("/my-followers")
    public TableDataInfo getMyFollowers(Long doctorId) {
        DoctorFollow followQuery = new DoctorFollow();
        followQuery.setDoctorId(doctorId);
        followQuery.setDel(0); // 只查未删除的关注
        List<DoctorFollow> list = doctorFollowService.selectDoctorFollowList(followQuery);
        return getDataTable(list);
    }*/
}
